﻿// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they bagin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
// 
// - Sun Tsu,
// "The Art of War"

using log4net.Core;

namespace Infrastructure.Logging.Appenders
{
    /// <summary>
    /// Appends log events to the <see cref="System.Diagnostics.Trace"/> system.
    /// </summary>
    /// <remarks>
    /// <para>
    /// This appender was created because DebugView puts the trace category infront of
    /// the message with is very annoying and hard to read.
    /// The appender inherits from the log4net TraceAppender overriding the write method
    /// to write only the message without the category.
    /// </para>
    /// <para>
    /// The application configuration file can be used to control what listeners 
    /// are actually used. See the MSDN documentation for the 
    /// <see cref="System.Diagnostics.Trace"/> class for details on configuring the
    /// trace system.
    /// </para>
    /// <para>
    /// Events are written using the <c>System.Diagnostics.Trace.Write(string,string)</c>
    /// method. The event's logger name is passed as the value for the category name to the Write method.
    /// </para>
    /// </remarks>
    public class TraceAppender : log4net.Appender.TraceAppender
    {
        /// <summary>
        /// Writes the logging event to the <see cref="System.Diagnostics.Trace"/> system.
        /// </summary>
        /// <param name="loggingEvent">The event to log.</param>
        /// <remarks>
        /// <para>
        /// Writes the logging event to the <see cref="System.Diagnostics.Trace"/> system.
        /// </para>
        /// </remarks>
        override protected void Append(LoggingEvent loggingEvent)
        {
            // Write the string to the Trace system
            System.Diagnostics.Trace.Write( RenderLoggingEvent( loggingEvent ) );

            // Flush the Trace system if needed
            if (ImmediateFlush)
                System.Diagnostics.Trace.Flush();
        }
    }
}
